package com.north.light.libmediapreview.ui;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.viewpager.widget.PagerAdapter;

import com.north.light.libmediapreview.R;
import com.north.light.libmediapreview.manager.PreviewLoadPicManager;
import com.north.light.libmediapreview.ui.base.PreviewBaseParentActivity;
import com.north.light.libmediapreview.widget.photoview.OnOutsidePhotoTapListener;
import com.north.light.libmediapreview.widget.photoview.PhotoView;
import com.north.light.libmediapreview.widget.viewpager.PreviewViewPager;

import java.util.ArrayList;
import java.util.List;


/**
 * 垂直预览图片
 */
public class PreviewPicLandscapeActivity extends PreviewBaseParentActivity {
    private static final String TAG = PreviewPicLandscapeActivity.class.getSimpleName();
    private RelativeLayout mRoot;
    private PreviewViewPager mViewPager;
    private ImageView mCancel;
    private List<PhotoView> mViewList = new ArrayList<>();
    private List<String> mDataList = new ArrayList<>();


    @Override
    protected void initData() {
        setFullScreen();
        initView();
        initEvent();
    }

    @Override
    protected void preInitView() {
        mRoot = findViewById(R.id.lib_media_preview_activity_preview_pic_landscape_root);
        mCancel = findViewById(R.id.lib_media_preview_activity_preview_pic_landscape_cancel);
        mRoot.setBackgroundColor(getResources().getColor(getIntentData().getPreviewBgResId()));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.lib_media_preview_activity_preview_pic_landscape;
    }

    @Override
    protected int screenOrg() {
        return 1;
    }

    @Override
    protected int statusBarColorRes() {
        return getIntentData().getPreviewBgResId();
    }

    private void initView() {
        mViewPager = findViewById(R.id.lib_media_preview_activity_preview_pic_landscape_vp);
        mViewPager.setAdapter(null);
        mViewList.clear();
        mDataList = getIntentData().getPreviewUrl();
        for (String result : mDataList) {
            PhotoView photoView = new PhotoView(this);
            mViewList.add(photoView);
            photoView.setOnOutsidePhotoTapListener(new OnOutsidePhotoTapListener() {
                @Override
                public void onOutsidePhotoTap(ImageView imageView) {
                    if (canCancelOutSide()) {
                        finish();
                    }
                }
            });
        }
        mViewPager.setOffscreenPageLimit(mDataList.size());
        mViewPager.setAdapter(pagerAdapter);
    }


    private void initEvent() {
        mRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private final PagerAdapter pagerAdapter = new PagerAdapter() {

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            return mViewList.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position,
                                Object object) {
            container.removeView(mViewList.get(position));
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "title";
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mViewList.get(position));
            try {
                PreviewLoadPicManager.getInstance().load(mDataList.get(position), mViewList.get(position));
            } catch (Exception e) {
                Log.d(TAG, "instantiateItem error: " + e.getMessage());
            }
            return mViewList.get(position);
        }

    };
}